import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  ValidationPipe,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
import { BaseCrudController } from '../common/controllers/base-crud.controller';
import { AgentsService } from './agents.service';
import { Agent } from './entities/agent.entity';
import { CreateAgentDto } from './dto/create-agent.dto';
import { UpdateAgentDto } from './dto/update-agent.dto';
import { PaginationDto } from '../common/dto/base.dto';

@ApiTags('agents')
@Controller('agents')
export class AgentsController extends BaseCrudController<
  Agent,
  CreateAgentDto,
  UpdateAgentDto
> {
  constructor(private readonly agentsService: AgentsService) {
    super(agentsService);
  }

  // 重写 create 方法以添加 Swagger 文档
  @Post()
  @ApiOperation({ summary: '创建Agent' })
  @ApiBody({ type: CreateAgentDto })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  async create(@Body() createDto: CreateAgentDto) {
    return await this.agentsService.create(createDto);
  }

  // 扩展接口：根据钱包地址查询
  @Get('wallet/:walletAddress')
  @ApiOperation({ summary: '根据钱包地址查询Agents' })
  async findByWallet(@Param('walletAddress') walletAddress: string) {
    return await this.agentsService.findByWallet(walletAddress);
  }

  // 扩展接口：搜索
  @Get('search/advanced')
  @ApiOperation({ summary: '高级搜索Agents' })
  async search(
    @Query('keyword') keyword?: string,
    @Query('tags') tags?: string,
    @Query('classification') classification?: string,
    @Query('minReputation') minReputation?: string,
    @Query(ValidationPipe) pagination?: PaginationDto,
  ) {
    const searchParams = {
      keyword,
      tags: tags ? tags.split(',') : undefined,
      classification,
      minReputation: minReputation ? parseFloat(minReputation) : undefined,
      pagination: pagination || new PaginationDto(),
    };

    return await this.agentsService.search(searchParams);
  }

  // 扩展接口：更新统计
  @Patch(':id/stats')
  @ApiOperation({ summary: '更新Agent统计信息' })
  async updateStats(
    @Param('id', ParseUUIDPipe) id: string,
    @Body('completed') completed: boolean,
  ) {
    return await this.agentsService.updateStats(id, completed);
  }
} 